/*
 * Project: Made 2 Order Software utility package
 * Package: com.made2ordersoftware.util
 * File: ExceptionHandler
 * Creation Date: Sep 25, 2009
 */

/*
 *  Copyright (c) 2010 Made 2 Order Software, LLC
 *
 *  All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 *  transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 *  You may not distribute the URL or redistribute the files to third parties. You may transfer the files
 *  onto a single storage device so long as you can prove ownership of the software. You may not reverse
 *  engineer, decompile, or disassemble the software or media files. You may not modify or translate the
 *  software or media, or distribute copies of the software or media without the written consent of Made
 *  2 Order Software
 */
package com.made2ordersoftware.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ExceptionHandler
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
public class ExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ExceptionHandler.class);
    private static String errPackage;
    private static String errClass;
    private static String errMethod;
    private static String errExceptionType;
    private static String errMessage;

    /**
     * Static method which takes an Exception object and passes it to handleException(Exception, Cause)
     *
     * @param e Exception to be processed.
     */
    public static void handleException(Exception e) {
        handleException(e.getMessage(), e.getCause());
    }

    /**
     * Static method which takes a String message and parses the message for logging and display purposes.
     *
     * @param message Message of Exception being processed.
     */
    public static void handleException(String message) {
        parseMessage(message + ":");
        //Uncomment out the following line for production
        //logException(cause);
    }

    /**
     * Static method which takes a String message and a Cause and parses the message for logging and display
     * purposes.
     *
     * @param message Message of Exception being processed.
     * @param cause   Cause of Exception being processed.
     */
    public static void handleException(String message, Throwable cause) {
        //parseMessage(message + ":");
        //Uncomment out the following line for production
        //logException(cause);
        //displayError(cause);
        //Remove the following 3 lines for production.
        LOG.info("[DEBUG] BEGIN STACK TRACE");
        LOG.info(message);
        LOG.info("[DEBUG] END STACK TRACE");
    }

    /**
     * Parses the exception message for logging and display purposes.
     *
     * @param message Message of Exception to be parsed.
     */
    private static void parseMessage(String message) {
        String[] error = message.split(":");
        errPackage = error[0];
        errClass = error[1];
        errMethod = error[2];
        errExceptionType = error[3];
        errMessage = error[4];
    }

    /**
     * Logs the exception to a file for debugging purposes.
     *
     * @param cause Cause of Exception to be logged to file.
     */
    private static void logException(Throwable cause) {
        // TODO Create method body.
    }

    /**
     * Displays the error message for development.
     *
     * @param cause Cause of Exception to be displayed in standard error System.err.
     */
    private static void displayError(Throwable cause) {
        LOG.info("[DEBUG] START OF DEBUG MESSAGE BELOW");
        LOG.info("[DEBUG] Package Name: {}", errPackage);
        LOG.info("[DEBUG] Class Name: {}", errClass);
        LOG.info("[DEBUG] Method Name: {}", errMethod);
        LOG.info("[DEBUG] Exception Type: {}", errExceptionType);
        LOG.info("[DEBUG] Error Message: {}", errMessage);
        LOG.info("[DEBUG] Exception: {}", cause);
        LOG.info("[DEBUG] END OF DEBUG MESSAGE");
    }

    /**
     * Static method to be used for logging and displaying error messages.
     *
     * @param e Exception needing to be handled.
     */
    public static void handleWarning(Exception e) {
        handleException(e.getMessage());
    }
}
